package com.iot.protocol.iec104.bean;

import lombok.Data;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

@Data
public class SqlSessionProvider {
    private static SqlSessionFactory factory = null;
    static {
        try {
            String config = "mybatis-config.xml";
            InputStream in = Resources.getResourceAsStream(config);
            factory = new SqlSessionFactoryBuilder().build(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession(){
        SqlSession session = null;
        if (factory != null){
            //此处设置为非自动提交事务
            session = factory.openSession();
        }
        return session;
    }
}
